package com.yklx.youke.controller;

import com.yklx.youke.entity.Booking;
import com.yklx.youke.service.BookingService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/api/bookings")
@CrossOrigin(origins = "*")
public class BookingController {

    private final BookingService bookingService;

    public BookingController(BookingService bookingService) {
        this.bookingService = bookingService;
    }

    @GetMapping
    public ResponseEntity<List<Map<String, Object>>> getUserBookings(
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String dateRange,
            @RequestParam(required = false) Boolean past) {
        
        // This would get bookings for the authenticated user based on their role
        // For now, returning a placeholder
        List<Map<String, Object>> bookings = bookingService.getUserBookings(status, dateRange, past);
        
        return ResponseEntity.ok(bookings);
    }

    @GetMapping("/{bookingId}")
    public ResponseEntity<Map<String, Object>> getBookingDetails(@PathVariable UUID bookingId) {
        try {
            Map<String, Object> bookingDetails = bookingService.getBookingDetails(bookingId);
            return ResponseEntity.ok(bookingDetails);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("error", e.getMessage()));
        }
    }

    @PostMapping
    public ResponseEntity<?> createBooking(@RequestBody Map<String, Object> requestData) {
        try {
            // Extract data from request
            UUID teacherId = UUID.fromString((String) requestData.get("teacherId"));
            UUID childId = UUID.fromString((String) requestData.get("childId"));
            String subject = (String) requestData.get("subject");
            String scheduledTimeStr = (String) requestData.get("scheduledTime");
            Integer duration = (Integer) requestData.get("duration");

            Map<String, Object> result = bookingService.createBooking(teacherId, childId, subject, scheduledTimeStr, duration);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("error", e.getMessage()));
        }
    }

    @PutMapping("/{bookingId}/confirm")
    public ResponseEntity<?> confirmBooking(@PathVariable UUID bookingId) {
        try {
            Map<String, Object> result = bookingService.confirmBooking(bookingId);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("error", e.getMessage()));
        }
    }

    @PutMapping("/{bookingId}/cancel")
    public ResponseEntity<?> cancelBooking(@PathVariable UUID bookingId) {
        try {
            Map<String, Object> result = bookingService.cancelBooking(bookingId);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("error", e.getMessage()));
        }
    }
}